
package Cart;

import DAO.DAO;
import DTO.Item;
import DTO.Promotion;
import Exception.KwickKioskDBException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * this class implements the functionalities defined in {@link IDiscountManager} interface
 * @author tejinder
 */
public class DiscountManager implements IDiscountManager{

     private static final Logger log =
            Logger.getLogger(DiscountManager.class.getName());
    private DAO dao;
    private  ArrayList<Promotion> promos = null;

    /**
     * constructor for this class
     * @param dao
     */
    public DiscountManager(DAO dao) {
        this.dao = dao;
    }

    /**
     * only item level discount is required for item details page, no need to check store card
     * @param item
     * @return
     */
    public Item checkDiscountOnItem(Item item){

         Promotion topPromo = new Promotion();
        try {
            promos = dao.getPromotionList(item.getUpc());
        } catch (KwickKioskDBException ex) {
            Logger.getLogger(DiscountManager.class.getName()).log(Level.SEVERE, null, ex);
        }

                if (promos!= null){
                    System.out.println("prmos are not null "+promos.size());
                    ListIterator<Promotion> promoIter = promos.listIterator();
                    while (promoIter.hasNext()){
                        Promotion tempPromo= promoIter.next();
                            // if promotion is largest yet, remember it
                            if(tempPromo.getAmount() > topPromo.getAmount())
                                topPromo = tempPromo;
                        }
                    } //At this point topPromo is the largest promotion associated with the item
                    //Set items discount amount to the top discount
                    double discount = topPromo.getAmount()*item.getRetailPrice();
                   System.out.println("setting discount inside discount mgr  "+discount);
                    item.setDiscountAmount(discount);

                     return item;
                }

     /**
     * Gets and sets the discounts for all the items in the result set
     */
    public  List<Item> setResultsDiscounts(List<Item> list, boolean isLoggedIn){
        List<Item> discountedItems = new ArrayList<Item>(list.size());
        ListIterator<Item> iter = list.listIterator();
       // boolean loggedIn = sessionMgr.getLoggedIn();

        // for all items in the result set, set thier discount amount
        while (iter.hasNext()){
            Item item = iter.next();
            ArrayList<Promotion> promos = null;
            try {
                // get the list of promotions associated with this item
                promos = dao.getPromotionList(item.getUpc());

                // if there are promotions associated with this item, set the items discount amount
                if (promos!= null){
                    // Iterate through all promos assc. with this item to get the biggest promo
                    ListIterator<Promotion> promoIter = promos.listIterator();
                    Promotion topPromo = new Promotion();
                    while (promoIter.hasNext()){
                        Promotion tempPromo= promoIter.next();
                        // Check for store card requirement
                        if (tempPromo.isStoreCardReq()){
                            // Only allow discount if customer is logged in
                            if (isLoggedIn){
                                // if Promotion is the largest yet, remember it
                                if(tempPromo.getAmount() > topPromo.getAmount())
                                    topPromo = tempPromo;
                            }
                        }
                        else{
                            // if promotion is largest yet, remember it
                            if(tempPromo.getAmount() > topPromo.getAmount())
                                topPromo = tempPromo;
                        }
                    } //At this point topPromo is the largest promotion associated with the item
                    //Set items discount amount to the top discount
                    double discount = topPromo.getAmount()*item.getRetailPrice();
                    item.setDiscountAmount(discount);
                    // if this is a store card discount, update storecard discount amount
                    if (topPromo.isStoreCardReq())
                        item.setStoreCardDiscount(discount);
                }

            } catch (KwickKioskDBException ex) {
                System.out.println("Please implement proper error handling for this");
                log.log(Level.SEVERE, null, ex);
            }
            discountedItems.add(item);
        }
        return  discountedItems;
    }
}

